﻿using Coolzon.Common;
using Coolzon.UI.Common;
using Coolzon.UI.Common.Event.Web;
using Coolzon.UI.Core;
using Coolzon.UI.Core.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Coolzon.UI.ViewerApp.Views
{
    /// <summary>
    /// Interaction logic for LoginView.xaml
    /// 用于做登录验证
    /// </summary>
    public partial class LoginView : View
    {
        /// <summary>
        /// 用户的账号
        /// </summary>
        public string UserAccount { get; set; }
        /// <summary>
        /// 用户的密码
        /// </summary>
        public string UserPassword { get; set; }

        /// <summary>
        /// 登陆类型
        /// </summary>
        public string SolutionSelectionMode { get; set; }

        public LoginView()
        {
            InitializeComponent();

            _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            WebEventBus.Register(this);
        }

        /// <summary>
        /// 创建登录界面，并且直接进行登录认证
        /// </summary>
        /// <param name="userAccount">账号</param>
        /// <param name="userPassword">密码</param>
        /// <param name="solutionSelectionMode">方案选择模式</param>
        public LoginView(string userAccount, string userPassword, string solutionSelectionMode)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = UserPassword;
            this.SolutionSelectionMode = solutionSelectionMode;
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            WebEventBus.Register(this);

            ctlBtnOK_Click(null, null);
        }

        public LoginView(string userAccount, string userPassword)
        {
            InitializeComponent();

            _ctlEditAccount.Text = userAccount;
            _ctlEditPassword.Password = UserPassword;
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;

            WebEventBus.Register(this);

            ctlBtnOK_Click(null, null);
        }

        protected override void OnActive()
        {
            base.OnActive();

            // 如果有打开的方案信息，需要再这里释放占用的资源
            // 回收资源
            if (UIContext.Current.RenderView != null)
            {
                // 1.去除RenderView里的所有组件的引用
                this.Dispatcher.Invoke(() =>
                {
                    (UIContext.Current.RenderView as RenderView).ClearAllParts();
                });
                // 2.去除组件创建缓存中的组件引用
                PartInstanceCache.ClearAllParts();
            }
            //GC.Collect();
        }

        protected override void OnDeactive()
        {
            WebEventBus.Unregister(this);

            base.OnDeactive();
        }

        protected override void OnClose()
        {
            WebEventBus.Unregister(this);
            base.OnClose();
        }

        /// <summary>
        /// 点击登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_ctlEditAccount.Text))
            {
                return;
            }
            
            _ctlBorderLogin.Visibility = System.Windows.Visibility.Hidden;
            _ctlBorderIndicator.Visibility = System.Windows.Visibility.Visible;

            UserAccount = _ctlEditAccount.Text;
            UserPassword = _ctlEditPassword.Password;
            
            // 登录到系统
            // 构造登录请求
            LoginBegin @event = new LoginBegin();
            @event.StartupPath = UIContext.Current.AppStartupPath;
            @event.AppType = UIContext.Current.AppType;
            @event.AppVersion = UIContext.Current.AppVersion;
            @event.OSVersion = UIContext.Current.OSVersion;
            @event.DotNetVersion = UIContext.Current.DotNetVersion;
            @event.UserAccount = UserAccount;
            @event.SolutionSelectionMode = SolutionSelectionMode;
            @event.UserPassword = UserPassword;
            @event.Endpoints = Config.Current.RealtimeEndpoints;

            WebEventBus.PublishAsync(@event);
        }

        private void ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            //GC.Collect();
            WebEventBus.Unregister(this);
            this.Cancel();
            Application.Current.Shutdown(); 
        }

        /// <summary>
        /// 全屏或退出全屏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _fullScreenImage_Click(object sender, RoutedEventArgs e)
        {
            //UIEventBus.Publish(new UIChangeFullScreenStateEvent());

        }
        
        #region ncl events
        [Subscribe]
        public void OnReceiveLoginEnd(LoginEnd @event)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (@event.State == WebEventState.Failure)
                {
                    System.Windows.MessageBox.Show("登录失败，出现错误。",
                        "错误",
                        System.Windows.MessageBoxButton.OK,
                        System.Windows.MessageBoxImage.Error);

                    _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                    _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
                }
                else if (@event.State == WebEventState.Finished)
                {
                    // 登录成功
                    UIContext.Current.UserAccount = UserAccount;
                    UIContext.Current.UserName = @event.UserName;
                    UIContext.Current.UserIdentity = @event.UserIdentity;
                    _ctlBorderLogin.Visibility = System.Windows.Visibility.Visible;
                    _ctlBorderIndicator.Visibility = System.Windows.Visibility.Hidden;
                    this.Submit();
                }
            });
        }
        #endregion
    }
}
